package edu.ynu.se.xiecheng.achitectureclass.controller;

import edu.ynu.se.xiecheng.achitectureclass.common.controller.LogicController;
import edu.ynu.se.xiecheng.achitectureclass.dao.PlantationDao;
import edu.ynu.se.xiecheng.achitectureclass.entity.Plantation;
import edu.ynu.se.xiecheng.achitectureclass.service.PlantationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

@Api(tags = "大棚管理接口")
@RestController
@RequestMapping("/plantation")
public class PlantationController extends LogicController<PlantationService, PlantationDao, Plantation, Long> {

    public PlantationController(@Autowired PlantationService service) {
        super(service);
    }

    @GetMapping("/info")
    @ApiOperation("获取单个大棚信息")
    public ResponseEntity<?> getPlantationInfo(@ApiParam("大棚ID") @RequestParam Long id) {
        try {
            Plantation plantation = getService().getPlantationInfo(id);

            // 构建返回数据
            Map<String, Object> plantationData = new HashMap<>();
            plantationData.put("id", plantation.getId());
            plantationData.put("plantationName", plantation.getPlantationName());
            plantationData.put("plantationAddress", plantation.getPlantationAddress());
            plantationData.put("plantationArea", plantation.getPlantationArea());
            plantationData.put("createTime", plantation.getCreateTime());
            plantationData.put("updateTime", plantation.getUpdateTime());

            // 处理设备信息
            if (plantation.getEquipments() != null) {
                plantationData.put("equipments", plantation.getEquipments().stream()
                        .map(equipment -> {
                            Map<String, Object> equipmentData = new HashMap<>();
                            equipmentData.put("id", equipment.getId());
                            equipmentData.put("equipmentName", equipment.getEquipmentName());
                            equipmentData.put("equipmentType", equipment.getEquipmentType());
                            equipmentData.put("equipmentSerial", equipment.getEquipmentSerial());
                            // ... 其他设备字段
                            return equipmentData;
                        })
                        .collect(Collectors.toList()));
            }

            return ResponseEntity.ok(Map.of(
                    "code", 200,
                    "message", "查询成功",
                    "data", plantationData
            ));
        } catch (Exception e) {
            return ResponseEntity.ok(Map.of(
                    "code", 400,
                    "message", e.getMessage()
            ));
        }
    }

    @PostMapping("/update")
    @ApiOperation("更新大棚信息")
    public ResponseEntity<?> updatePlantation(@Valid @RequestBody Plantation plantation) {
        try {
            Plantation updatedPlantation = getService().updatePlantation(plantation);
            return ResponseEntity.ok(Map.of(
                    "code", 200,
                    "message", "更新成功",
                    "data", updatedPlantation
            ));
        } catch (Exception e) {
            return ResponseEntity.ok(Map.of(
                    "code", 400,
                    "message", e.getMessage()
            ));
        }
    }
}